package com.btl.service.ai.support.rag.generation;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.listener.ChatModelErrorContext;
import dev.langchain4j.model.chat.listener.ChatModelListener;
import dev.langchain4j.model.chat.listener.ChatModelRequestContext;
import dev.langchain4j.model.chat.listener.ChatModelResponseContext;
import dev.langchain4j.model.openaiofficial.OpenAiOfficialChatModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Slf4j
@Configuration(proxyBeanMethods = false)
public class GenerationModelProvider {

    public ChatModelListener chatModelListener() {
        return new ChatModelListener() {
            @Override
            public void onRequest(ChatModelRequestContext requestContext) {
                log.info("request：{}", requestContext.chatRequest());
            }

            @Override
            public void onResponse(ChatModelResponseContext responseContext) {
                log.info("response：{}", responseContext.chatResponse());
            }

            @Override
            public void onError(ChatModelErrorContext errorContext) {
                log.error("error：{}", String.valueOf(errorContext.error()));
            }
        };
    }


    @Bean
    public ChatModel iOpenAiChatModel() {
        return OpenAiOfficialChatModel.builder()
                .listeners(List.of(chatModelListener()))
                .modelName(com.openai.models.ChatModel.GPT_4O_MINI)
                .baseUrl("http://139.59.125.71:3000/v1/")
                .apiKey("sk-hFTLLBHYuUuzbXOjHQmJDx1APfKrxa2ipWkk1BufXYzQQh2A")
                .build();
    }

    @Bean
    public ChatModel iOpenAiChatFormatResponseModel() {
        return OpenAiOfficialChatModel.builder()
                .listeners(List.of(chatModelListener()))
                .responseFormat("json_object")
                .modelName(com.openai.models.ChatModel.GPT_4O_MINI)
                .baseUrl("http://139.59.125.71:3000/v1/")
                .apiKey("sk-hFTLLBHYuUuzbXOjHQmJDx1APfKrxa2ipWkk1BufXYzQQh2A")
                .build();
    }

    @Bean
    public ChatModel iQwenChatModel() {
        return QwenChatModel.builder()
//                .apiKey("sk-b41614f0eaca4092bbe3cfb7a23e0e1f")
                .apiKey("sk-eec56f4cea7a4db2a9df954ce2097eab")
                .modelName("qwen-plus")
                .temperature(0.7F)
                .maxTokens(4096)
//                .defaultRequestParameters(ChatRequestParameters
//                        .builder()
//                        .responseFormat(ResponseFormat.JSON)
//                        .build())
                .listeners(List.of(chatModelListener()))
                .build();
    }
}